﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;
using System.Drawing.Imaging;
using Meshomatic;

using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;


namespace Reconstructor
{
    class ModelDataBuilder
    {
        public MeshData mesh;
        public uint texture;

        private bool meshLoaded;
        private bool textureLoaded;

        ObjLoader objLoader;
        Ms3dLoader ms3Loader;
        ColladaLoader colladaLoader;

        public bool AllDataLoaded
        {
            get { return meshLoaded && textureLoaded; }
        }

        public ModelDataBuilder()
        {
            objLoader = new ObjLoader();
            ms3Loader = new Ms3dLoader();
            colladaLoader = new ColladaLoader();
        }
        public void LoadMesh(Stream stream, FileFormat format)
        {
            switch (format)
            {
                case FileFormat.obj:
                    mesh = objLoader.LoadStream(stream);
                    meshLoaded = true;
                    break;
                case FileFormat.ms3d:
                    mesh = ms3Loader.LoadStream(stream);
                    meshLoaded = true;
                    break;
                case FileFormat.dae:
                    mesh = colladaLoader.LoadStream(stream);
                    meshLoaded = true;
                    break;
            }
        }

        public void LoadTexture(Stream stream)
        {
            texture = LoadTex(new Bitmap(stream, false));
            textureLoaded = true;
        }

        private uint LoadTex(Bitmap bitmap)
        {
            if (!IsPowerOf2(bitmap))
            {
                // XXX: FormatException isn't really the best here, buuuut...
                throw new FormatException("Texture sizes must be powers of 2!");
            }

            uint texture;
            GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);

            GL.GenTextures(1, out texture);
            GL.BindTexture(TextureTarget.Texture2D, texture);

            BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
                OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
            bitmap.UnlockBits(data);

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);

            return texture;
        }

        private bool IsPowerOf2(Bitmap b)
        {
            //uint w = bitmap.Width;
            //uint h = bitmap.Height;

            return true;
        }


    }
}
